package com.songlanyun.modules.rabbitmq.interactmsg.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.songlanyun.common.enums.MarketingConstant;
import com.songlanyun.common.enums.PrivateMessageConstant;
import com.songlanyun.common.utils.push.GeTuiUtils;
import com.songlanyun.common.utils.push.Msg;
import com.songlanyun.modules.account.service.AccountService;
import com.songlanyun.modules.marketing.groupbuy.entity.GroupBuyActivity;
import com.songlanyun.modules.marketing.groupbuy.service.GroupBuyActivityService;
import com.songlanyun.modules.message.entity.PrivateMessage;
import com.songlanyun.modules.message.model.ActivityMessage;
import com.songlanyun.modules.message.model.to.MessageTO;
import com.songlanyun.modules.message.model.to.PrivateMessageTO;
import com.songlanyun.modules.message.service.PrivateMessageService;
import com.songlanyun.modules.rabbitmq.interactmsg.IInteractMsgService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * 拼团   --  活动消息
 */
@Service("groupBuyInteractService")
public class GroupBuyInteractServiceImpl implements IInteractMsgService {

    @Resource
    private GroupBuyActivityService groupBuyActivityService;

    @Resource
    private AccountService accountService;

    @Resource
    private PrivateMessageService privateMessageService;

    @Resource
    private GeTuiUtils geTuiUtils;

    @Override
    public void savePrivateMsg(MessageTO to) {

        GroupBuyActivity groupBuyActivity = groupBuyActivityService.getById(to.getModelId());
        //发送站内消息    给商家下的所有会员
//        List<Long> userIds = accountService.getMemberBySubShop(groupBuyActivity.getShopId());
        //查询所有的会员
        List<Long> userIds = accountService.getAllMemberId();

        List<PrivateMessage> list = new ArrayList<>();
        String remark = String.format("拼团活动【%s】即将开始，快来参加吧！", groupBuyActivity.getName());
        userIds.forEach(u -> {
            ActivityMessage activityMessage = new ActivityMessage(MarketingConstant.ActivityType.GROUP_BUY, groupBuyActivity.getId(), groupBuyActivity.getName(), groupBuyActivity.getImgUrl());
            PrivateMessage privateMessage = new PrivateMessage(
                    new PrivateMessageTO(
                            u,
                            PrivateMessageConstant.MessageType.ACTIVITY_MESSAGE,
                            activityMessage,
                            remark)
            );
            list.add(privateMessage);
        });

        privateMessageService.saveBatch(list);

        //批量推
        if (CollectionUtil.isNotEmpty(list)) {
            geTuiUtils.pushByUserIds(
                    Msg.builder()
                            .uidList(userIds)
                            .title(PrivateMessageConstant.MessageType.ACTIVITY_MESSAGE.getMsg())
                            .content(remark)
                            .msg(list.get(0).pushMessageBody())
                            .build()
            );
        }

    }
}
